//
//  ViewController.swift
//  CollapsibleTtableSection
//
//  Created by apple on 16/12/26.
//  Copyright © 2016年 com.zaoguankeji.www. All rights reserved.
//

import UIKit

class ViewController: UITableViewController {

    var section:[Section] = {
    
    return sections
    
    }()
    
//    let tableViewa:UITableView = {
//        let tableView = UITableView(frame: CGRect.zero, style: .plain)
//        tableView.backgroundColor = .clear
//        tableView.showsVerticalScrollIndicator = false
//        tableView.showsHorizontalScrollIndicator = false
//        return tableView
//    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let os = ProcessInfo().operatingSystemVersion
        switch (os.majorVersion, os.minorVersion, os.patchVersion) {
        case (8, _, _):
            print("iOS >= 8.0.0, < 8.1.0")
        case (8, _, _):
            print("iOS >= 8.1.0, < 9.0")
        case (9, _, _):
            print("iOS >= 9.0.0")
        case (10, _, _):
            print("iOS >= 10.0.0")
        default:
            // this code will have already crashed on iOS 7, so >= iOS 10.0
            print("iOS >= 10.0.0")
        }

        tableView.backgroundColor = .white
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false

        tableView.delegate = self
        tableView.dataSource = self
        automaticallyAdjustsScrollViewInsets = true
        navigationController?.navigationBar.isTranslucent = false
//        view.addSubview(tableViewa)
        // Do any additional setup after loading the view, typically from a nib.
    }
    
  
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    
}
 ///MARK -- View Controller DataSource and Delegate

extension ViewController{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return sections[section].items.count
    
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell? ?? UITableViewCell(style: .default, reuseIdentifier: "cell")
        
        cell.textLabel?.text = sections[indexPath.section].items[indexPath.row]
        
        return cell

        
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
         return sections[indexPath.section].collapsed! ? 0 : 44.0
        
    }
    
    // Header
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header")
        
        header.titleLabel.text = sections[section].name
        header.arrowLabel.text = ">"
        header.setCollapsed(collapsed: sections[section].collapsed)
        
        header.section = section
        header.btnClickBlock = {
        (header,section)in
            let collapsed = !sections[section].collapsed
            sections[section].collapsed = collapsed
            header.setCollapsed(collapsed: collapsed)
            tableView.beginUpdates()
            for i in 0..<sections[section].items.count {
                tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
                
            }
            tableView.endUpdates()
        }

        return header
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44.0
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 1.0
    }


}


extension ViewController: CollapsibleTableViewHeaderDelegate {
     func toggleSection(_ header: CollapsibleTableViewHeader, section: Int) {
    let collapsed = !sections[section].collapsed
        sections[section].collapsed = collapsed
        header.setCollapsed(collapsed: collapsed)
        tableView.beginUpdates()
        for i in 0..<sections[section].items.count {
            tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
            
        }
        tableView.endUpdates()
        
        
        
    }
    
    
}
